package com.example.liang.basamovie.adapter;

import android.content.Context;
import android.graphics.BitmapFactory;
import android.graphics.drawable.Drawable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.example.liang.basamovie.R;
import com.example.liang.basamovie.bean.FindFilmCasts;
import com.example.liang.basamovie.bean.FindFilmInfo;
import com.example.liang.basamovie.bean.FindFilmInfoResources;
import com.example.liang.basamovie.bean.FindFilmRelationArticle;
import com.example.liang.basamovie.bean.FindFilmUserComment;
import com.example.liang.basamovie.bean.FindMovieInfo;
import com.example.liang.basamovie.utils.ImageLoaderCircleUtils;
import com.example.liang.basamovie.utils.ImageLoaderUtils;
import com.example.liang.basamovie.utils.MyAnimation;

import java.util.List;
import java.util.Random;

/**
 * 电影详情的适配器
 */
public class FindFilmInfoAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    FindFilmInfo filmInfo;
    Context context;
    List<FindFilmUserComment> commentList;
    TextView filmTitle;

    public FindFilmInfoAdapter(FindFilmInfo filmInfo, Context context,TextView filmTitle) {
        this.filmInfo = filmInfo;
        this.context = context;
        this.filmTitle=filmTitle;
        setSize();
    }

    private void setSize() {
        commentList=filmInfo.getFindFilmUserCommentList();
    }

    @Override
    public int getItemViewType(int position) {
        if(position==0){
            return R.layout.fragment_find_filminfo_layout;
        }else if(position==1){
            return R.layout.fragment_find_filmsummary_layout;
        }else if(position==2){
            return R.layout.fragment_find_filmplayer_layout;
        }else if(position==3){
            return R.layout.fragment_findcommentcount_layout;
        }else {
            return R.layout.fragment_find_userscomment_layout;
        }
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(viewType, parent, false);
        if(viewType==R.layout.fragment_find_filminfo_layout){
            return new MyViewHolder1(view);
        }else if(viewType==R.layout.fragment_find_filmsummary_layout){
            return new MyViewHolder2(view);
        }else if(viewType==R.layout.fragment_find_filmplayer_layout){
            return new MyViewHolder3(view);
        }else if(viewType==R.layout.fragment_findcommentcount_layout){
            return new MyViewHolder5(view);
        }else {
            return new MyViewHolder4(view);
        }
    }

    @Override
    public void onBindViewHolder(final RecyclerView.ViewHolder holder, int position) {
        //获取演员集合
        List<FindFilmCasts> CastsList = filmInfo.getFindFilmCastsList();
        FindFilmInfoResources FilmResources = filmInfo.getFindFilmInfoResources();
        FindMovieInfo movieInfo = filmInfo.getFindMovieInfo();
        //List<FindFilmRelationArticle> relationArticleList = filmInfo.getFindFilmRelationArticleList();
        if(position==0){
            if(holder instanceof MyViewHolder1){
                //((MyViewHolder1)holder).find_filminfoImageView;
                ImageLoaderUtils.getimage(context,FilmResources.getImageUrl(),((MyViewHolder1)holder).find_filminfoImageView);
                ((MyViewHolder1)holder).find_filminfoTitle.setText(FilmResources.getTitle()+"("+FilmResources.getYear()+")");
                ((MyViewHolder1)holder).find_filminfoSecondTitle.setText(movieInfo.getTitle_pinyin());
                ((MyViewHolder1)holder).find_filminfoconCountries.setText("国家："+movieInfo.getCountries());
                ((MyViewHolder1)holder).find_filminfoType.setText("类型："+movieInfo.getGenres());
                ((MyViewHolder1)holder).find_filminfoTime.setText("时长："+movieInfo.getDurations());
                ((MyViewHolder1)holder).find_filminData.setText(movieInfo.getPubdates());
                filmTitle.setText(FilmResources.getTitle());
                ImageLoaderUtils.getimage(context,FilmResources.getImageUrl(),((MyViewHolder1)holder).finf_filmBackground);

                final boolean[] flag1 = {true};
                final boolean[] flag2 = {true};
                final boolean[] flag3 = {true};
                ((MyViewHolder1)holder).isLook.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if(flag1[0]){
                            ((MyViewHolder1)holder).isLookImage.setImageResource(R.drawable.iv_video_detailsz_zan_y);
                            flag1[0] =false;
                            Toast.makeText(context,"添加想看成功",Toast.LENGTH_LONG).show();
                        }else {
                            ((MyViewHolder1)holder).isLookImage.setImageResource(R.drawable.iv_details_santtsee);
                            flag1[0] =true;
                            Toast.makeText(context,"删除想看成功",Toast.LENGTH_LONG).show();
                        }
                    }
                });
                ((MyViewHolder1)holder).NoLook.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if(flag2[0]){
                            ((MyViewHolder1)holder).NoLookImage.setImageResource(R.drawable.movie_btn_new_jk_y);
                            flag2[0] =false;
                            Toast.makeText(context,"添加已看成功",Toast.LENGTH_SHORT).show();
                        }else {
                            ((MyViewHolder1)holder).NoLookImage.setImageResource(R.drawable.movie_btn_new_jk_n);
                            flag2[0] =true;
                            Toast.makeText(context,"删除已看成功",Toast.LENGTH_SHORT).show();
                        }
                    }
                });
                ((MyViewHolder1)holder).GiveOtherLook.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Toast.makeText(context,"推荐跳转事件",Toast.LENGTH_SHORT).show();
                    }
                });



            }
        }else if(position==1){
            if(holder instanceof MyViewHolder2){
                ((MyViewHolder2)holder).textView.setText(FilmResources.getSummary());
            }
        }else if(position==2){
            //演员横向滑动
            if(holder instanceof MyViewHolder3){
                LinearLayoutManager manager=new LinearLayoutManager(context,LinearLayoutManager.HORIZONTAL,false);
                ((MyViewHolder3)holder).recyclerView.setLayoutManager(manager);
                ((MyViewHolder3)holder).recyclerView.setAdapter(new FindFilmPlayerApater(CastsList,context));

            }
        }else if(position==3){
            if(holder instanceof MyViewHolder5 ){
                ((MyViewHolder5)holder).textView.setText("用户评论"+commentList.size()+"条");
            }
        }else {
            if(holder instanceof MyViewHolder4){
                FindFilmUserComment userComment = commentList.get(position - 4);
                ((MyViewHolder4)holder).UserName.setText(userComment.getNickname());
                ((MyViewHolder4)holder).LikeCount.setText(userComment.getPraisenum()+"");
                ((MyViewHolder4)holder).Conment.setText(userComment.getContent());
                if(userComment.getHeadImgUrl().trim()!=null){
                    //Log.e("url" ,"---?"+userComment.getHeadImgUrl());
                    ImageLoaderCircleUtils.getimage(context,userComment.getHeadImgUrl(),((MyViewHolder4)holder).imageView);
                }
                ((MyViewHolder4)holder).CoomentTime.setText(((int)(Math.random()*10+1)+"小时前"));
                //喜欢评论数目的点击事件
                final boolean[] isLike = {true};
                ((MyViewHolder4)holder).find_commentLikeImage.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if(isLike[0]){
                            ((MyViewHolder4)holder).find_commentLikeImage.setImageResource(R.drawable.zan_press_new);
                            isLike[0] =false;
                            String string = ((MyViewHolder4)holder).LikeCount.getText().toString();
                            int newNum = Integer.parseInt(string)+1;
                            ((MyViewHolder4)holder).LikeCount.setText(newNum+"");

                        }else{
                            ((MyViewHolder4)holder).find_commentLikeImage.setImageResource(R.drawable.zan_new_white);
                            isLike[0] =true;
                            String string = ((MyViewHolder4)holder).LikeCount.getText().toString();
                            int newNum = Integer.parseInt(string)-1;
                            ((MyViewHolder4)holder).LikeCount.setText(newNum+"");
                        }
                        MyAnimation.SetAnimation(((MyViewHolder4)holder).find_commentLikeImage);
                    }
                });
            }
        }



    }

    @Override
    public int getItemCount() {
        return commentList.size()+4;
    }
    //电影详情
    class MyViewHolder1 extends RecyclerView.ViewHolder{
        ImageView find_filminfoImageView,finf_filmBackground;
        TextView find_filminfoTitle,find_filminfoSecondTitle,find_filminfoconCountries
                ,find_filminfoType,find_filminfoTime,find_filminData;
        RelativeLayout isLook,NoLook,GiveOtherLook;
        ImageView isLookImage,NoLookImage,GiveOtherLookImage;
        public MyViewHolder1(View itemView) {
            super(itemView);
            find_filminfoImageView= (ImageView) itemView.findViewById(R.id.find_filminfoImageView);
            find_filminfoTitle= (TextView) itemView.findViewById(R.id.find_filminfoTitle);
            find_filminfoSecondTitle= (TextView) itemView.findViewById(R.id.find_filminfoSecondTitle);
            find_filminfoconCountries= (TextView) itemView.findViewById(R.id.find_filminfoconCountries);
            find_filminfoType= (TextView) itemView.findViewById(R.id.find_filminfoType);
            find_filminfoTime= (TextView) itemView.findViewById(R.id.find_filminfoTime);
            find_filminData= (TextView) itemView.findViewById(R.id.find_filminData);
            finf_filmBackground= (ImageView) itemView.findViewById(R.id.finf_filmBackground);

            isLook= (RelativeLayout) itemView.findViewById(R.id.isLook);
            NoLook= (RelativeLayout) itemView.findViewById(R.id.NoLook);
            GiveOtherLook= (RelativeLayout) itemView.findViewById(R.id.GiveOtherLook);
            isLookImage= (ImageView) itemView.findViewById(R.id.isLookImage);
            NoLookImage= (ImageView) itemView.findViewById(R.id.NoLookImage);
            GiveOtherLookImage= (ImageView) itemView.findViewById(R.id.GiveOtherLookImage);

        }
    }
    //剧情简介
    class MyViewHolder2 extends RecyclerView.ViewHolder{
        TextView textView;

        public MyViewHolder2(View itemView) {
            super(itemView);
     textView= (TextView) itemView.findViewById(R.id.find_findSummary);
        }
    }
    //演员
    class MyViewHolder3 extends RecyclerView.ViewHolder{
        RecyclerView recyclerView;
        public MyViewHolder3(View itemView) {
            super(itemView);
            recyclerView= (RecyclerView) itemView.findViewById(R.id.find_filmPlayerRecyclerView);
        }
    }
    //评论
    class MyViewHolder4 extends RecyclerView.ViewHolder{
        ImageView imageView,find_commentLikeImage;
        TextView UserName,LikeCount,Conment,CoomentTime;
        public MyViewHolder4(View itemView) {
            super(itemView);
            imageView= (ImageView) itemView.findViewById(R.id.find_commentUserHeadImage);
            UserName= (TextView) itemView.findViewById(R.id.find_commentUserName);
            LikeCount= (TextView) itemView.findViewById(R.id.find_commentLikeCount);
            Conment= (TextView) itemView.findViewById(R.id.find_commentConment);
            CoomentTime= (TextView) itemView.findViewById(R.id.commentTime);
            find_commentLikeImage= (ImageView) itemView.findViewById(R.id.find_commentLikeImage);

        }
    }
    class MyViewHolder5 extends RecyclerView.ViewHolder{
        TextView textView;
        public MyViewHolder5(View itemView) {
            super(itemView);
            textView= (TextView) itemView.findViewById(R.id.find_commentCount);
        }
    }
}
